

package cn.shengchao.examstar.distribute.model;

import cn.hutool.core.bean.BeanUtil;
import cn.shengchao.examstar.rpc.distribute.cs.UserCs;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeInfo;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeRole;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeRoleChain;
import cn.shengchao.examstar.rpc.distribute.dto.DistributeNodeBaseCreateDto;
import cn.shengchao.examstar.rpc.distribute.enums.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import top.continew.starter.core.exception.BaseException;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @program: examstarapi
 * @description: 分销链体系建立模型
 * @author: Xiong weiqiang
 * @create: 2025-03-26 12:20
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class DistributeNodeRoleChainCreator extends DistributeNodeInfo {
    private DistributeNodeInfo upLevelDistributeNodeInfo;
    private BigDecimal percentStable;

    public static DistributeNodeRoleChainCreator buildCreatorFromCreateDto(DistributeNodeBaseCreateDto distributeNodeInfoCreateDto,
                                                                           DistributeNodeInfo upLevelDistributeNodeInfo) {
        DistributeNodeRoleChainCreator distributeNodeRoleChainCreator = new DistributeNodeRoleChainCreator();
        BeanUtil.copyProperties(distributeNodeInfoCreateDto, distributeNodeRoleChainCreator);
        distributeNodeRoleChainCreator.setUpLevelDistributeNodeInfo(upLevelDistributeNodeInfo);
        return distributeNodeRoleChainCreator;
    }

    public DistributeNodeRoleChain buildNodeRoleChain() {
        DistributeNodeRoleChain distributeNodeRoleChain = new DistributeNodeRoleChain();
        distributeNodeRoleChain.setDistributeType(getDistributeType());
        distributeNodeRoleChain.setDistributeNodeId(getId());
        distributeNodeRoleChain.setRefUserId(getUserId());
        distributeNodeRoleChain.setUpLevelNodeId(upLevelDistributeNodeInfo.getId());
        distributeNodeRoleChain.setRefRoleCode(getDistributeRoleCode());
        Integer distributeRoleLevel= null;
        if (getDistributeType().equals(DistributeTypeEnum.TYPE_SALE.getType())){
            DistributeSaleRoleEnum roleEnum = DistributeSaleRoleEnum.getByCode(getDistributeRoleCode());
            distributeRoleLevel=roleEnum.getDistributeLevel();
            if (roleEnum == null) {
                throw new BaseException("构建分销模型时：链记录角色枚举查找错误");
            }
        }else if (getDistributeType().equals(DistributeTypeEnum.TYPE_CTPD.getType())){
            DistributeContentRoleEnum roleEnum = DistributeContentRoleEnum.getByCode(getDistributeRoleCode());
            distributeRoleLevel=roleEnum.getDistributeLevel();
            if (roleEnum == null) {
                throw new BaseException("构建分销模型时：链记录角色枚举查找错误");
            }
        }



        distributeNodeRoleChain.setDistributeRoleLevel(distributeRoleLevel);
        distributeNodeRoleChain.setDataStatus(DistributeDataStatusEnum.STATUS_CURT.getStatus());
        return distributeNodeRoleChain;
    }

    public DistributeNodeRole buildRoleInfo() {
        DistributeNodeRole distributeNodeRole = new DistributeNodeRole();
        distributeNodeRole.setUserId(getUserId());
        distributeNodeRole.setDistributeType(getDistributeType());
        distributeNodeRole.setRoleCode(getDistributeRoleCode());
        Integer distributeRoleLevel= null;
        if (getDistributeType().equals(DistributeTypeEnum.TYPE_SALE.getType())){
            DistributeSaleRoleEnum roleEnum = DistributeSaleRoleEnum.getByCode(getDistributeRoleCode());
            distributeRoleLevel=roleEnum.getDistributeLevel();
            if (roleEnum == null) {
                throw new BaseException("构建分销模型时：链记录角色枚举查找错误");
            }
        }else if (getDistributeType().equals(DistributeTypeEnum.TYPE_CTPD.getType())){
            DistributeContentRoleEnum roleEnum = DistributeContentRoleEnum.getByCode(getDistributeRoleCode());
            distributeRoleLevel=roleEnum.getDistributeLevel();
            if (roleEnum == null) {
                throw new BaseException("构建分销模型时：链记录角色枚举查找错误");
            }
        }
        distributeNodeRole.setUpLevel(distributeRoleLevel);
        distributeNodeRole.setDataStatus(DistributeDataStatusEnum.STATUS_CURT.getStatus());
        distributeNodeRole.setValidTime(new Date());
        distributeNodeRole.setPercentStable(getPercentStable());
        return distributeNodeRole;
    }

    public static DistributeNodeInfo buildPlatformNodeInfo() {
        DistributeNodeInfo distributeNodeInfo = new DistributeNodeInfo();
        distributeNodeInfo.setId(UserCs.PLATFORM_USER_ID);
        distributeNodeInfo.setUserId(UserCs.PLATFORM_USER_ID);
        distributeNodeInfo.setUserType(DistributeNodePropEnum.TYPE_O.getProp());
        distributeNodeInfo.setDistributeRoleCode(DistributeSaleRoleEnum.ROLE_YSXG.getCode());
        distributeNodeInfo.setDistributeRoleLevel(DistributeSaleRoleEnum.ROLE_YSXG.getDistributeLevel());
        distributeNodeInfo.setShortName("平台");
        distributeNodeInfo.setFullName("平台");
        distributeNodeInfo.setHeadImageUrl("");
        distributeNodeInfo.setContactPhone("");
        distributeNodeInfo.setContactMan("");
        distributeNodeInfo.setPInLaw("");
        return distributeNodeInfo;
    }

}